using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using iTron.Model.GridItems;


namespace iTron.Model
{
    public class Grid
    {
        public const int GRID_HEIGHT = 16, GRID_WIDTH = 16;
        public GridItem[,] grid { get; set; }
        

        public Grid()
        {
            initGrid();
        }

        public void addPlayer(Player.Player player)
        {
            Bike bike;
            switch (player.ID)
            {
                case 1:
                    bike = new Bike(Utils.DIRECTION_TOP, player.ID, (GridItem.GRIDITEM_WIDTH / 2) - 1, GridItem.GRIDITEM_HEIGHT - 1);
                    grid[GRID_HEIGHT - 1, (GRID_WIDTH / 2) - 1] = bike;
                    break;
                case 2:
                    bike = new Bike(Utils.DIRECTION_BOTTOM, player.ID, (GridItem.GRIDITEM_WIDTH / 2) - 1, 0);
                    grid[0, (GRID_WIDTH / 2) - 1] = bike;
                    break;
                case 3:
                    bike = new Bike(Utils.DIRECTION_BOTTOM, player.ID, (GridItem.GRIDITEM_WIDTH / 2) - 1, 0);
                    grid[0, (GRID_WIDTH / 2) - 3] = bike;
                    break;
                case 4:
                    bike = new Bike(Utils.DIRECTION_BOTTOM, player.ID, (GridItem.GRIDITEM_WIDTH / 2) - 1, 0);
                    grid[0, (GRID_WIDTH / 2) + 1] = bike;
                    break;
                case 5:
                    bike = new Bike(Utils.DIRECTION_BOTTOM, player.ID, (GridItem.GRIDITEM_WIDTH / 2) - 1, 0);
                    grid[0, (GRID_WIDTH / 2) - 5] = bike;
                    break;
                case 6:
                    bike = new Bike(Utils.DIRECTION_BOTTOM, player.ID, (GridItem.GRIDITEM_WIDTH / 2) - 1, 0);
                    grid[0, (GRID_WIDTH / 2) + 3] = bike;
                    break;
                default:
                    break;
            }
        }

        private void initGrid() {
            grid = new GridItem[GRID_HEIGHT, GRID_WIDTH];

            for (int height = 0; height < GRID_HEIGHT; height++)
            {
                for (int width = 0; width < GRID_WIDTH; width++)
                {
                    grid[height, width] = new Empty();
                }
            }
        }

        public void turnBike(Player.Player player, int direction)
        {
            for (int i = 0; i < Grid.GRID_HEIGHT; i++)
            {
                for (int j = 0; j < Grid.GRID_WIDTH; j++)
                {
                    if (grid[i, j].isBike && grid[i, j].playerID == player.ID)
                    {
                        if ((grid[i, j].x == 3 || grid[i, j].x == 4) &&
                            (grid[i, j].y == 3 || grid[i, j].y == 4))
                        {
                            // TODO: Not working
                            switch (grid[i, j].directionFrom)
                            {
                                case Utils.DIRECTION_BOTTOM:
                                    if (direction == Utils.DIRECTION_LEFT) grid[i, j].directionTo = Utils.DIRECTION_RIGHT;
                                    else grid[i, j].directionTo = Utils.DIRECTION_LEFT;
                                    break;
                                case Utils.DIRECTION_LEFT:
                                    if (direction == Utils.DIRECTION_LEFT) grid[i, j].directionTo = Utils.DIRECTION_BOTTOM;
                                    else grid[i, j].directionTo = Utils.DIRECTION_TOP;
                                    break;
                                case Utils.DIRECTION_RIGHT:
                                    if (direction == Utils.DIRECTION_LEFT) grid[i, j].directionTo = Utils.DIRECTION_TOP;
                                    else grid[i, j].directionTo = Utils.DIRECTION_BOTTOM;
                                    break;
                                case Utils.DIRECTION_TOP:
                                    if (direction == Utils.DIRECTION_LEFT) grid[i, j].directionTo = Utils.DIRECTION_LEFT;
                                    else grid[i, j].directionTo = Utils.DIRECTION_RIGHT;
                                    break;
                                default:
                                    grid[i, j].directionTo = grid[i, j].directionFrom;
                                    break;
                            }
                            player.nextAction = 0;
                        }
                    }
                }
            }
        }
    }
}
